// pages/main_lib/main_lib.js
const baseURL = 'http://100.2.178.56:8080'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        stuId: null,
        stuName: '',
        grade: '',
        academy: '',
        reserveState: '',
        advices: '',
        seatCount: '',
        stuPhone: '',
        reserve: null,
        seaNum: '',
        floNum: '',
        honestGrade: '',

        // 教师部分
        teaName: null,
        teaPhone: ''
    },
    /**
     * 修改绑定
     * @param {*} options 
     */
    update_bind: function () {
        wx.navigateTo({
            url: '/pages/update_bind_student/update_bind_student',
        })
    },
    update_bind_tea: function () {
        wx.navigateTo({
            url: '/pages/update_bind_tea/update_bind_tea',
        })
    },

    update_information: function () {
        wx.navigateTo({
            url: '/pages/update_information_student/update_information_student',
        })
    },
    update_information_tea: function () {
        wx.navigateTo({
            url: '/pages/update_information_tea/update_information_tea',
        })
    },
    advice_manage: function () {
        wx.navigateTo({
            url: '/pages/advice_manage/advice_manage',
        })
    },
    reserveCancel_click: function () {
        var that = this

        wx.showModal({
            title: '提示',
            content: '确定取消预约吗？',
            success(res) {
                if (res.confirm) {
                    wx.request({
                        url: baseURL + '/studentSeatRelation/cancel',
                        method: "GET",
                        header: {
                            'Authorization': getApp().token
                        },
                        data: {
                            "stuSeaId": that.data.reserve.stuSeaId,
                            "stuUseId": that.data.reserve.stuUserId,
                        },
                        success: function (res) {
                            getApp().user_reserve = null
                            getApp().seaNum = null
                            getApp().floNum = null
                            that.setData({
                                reserve:null
                            })
                            that.onShow()

                        }
                    })
                } else if (res.cancel) {}
            }
        })



    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        var that = this;
        //获取座位剩余数
        wx.request({
            url: baseURL + '/seat/count',
            method: "GET",
            success: function (res) {
                var resData = res.data;
                if (resData.code == 0) {
                    that.setData({

                        seatCount: resData.data.seatCount
                    });
                } else {
                    wx.showToast({
                        title: resData.message,
                        duration: 2000
                    })
                }
            }
        })
        /**
         * 获取所有公告
         */
        wx.request({
            url: baseURL + '/advice/list',
            method: "GET",
            success: function (res) {
                var resData = res.data;
                if (resData.code == 0) {
                    that.setData({
                        advices: resData.data.advices,
                    });
                } else {
                    wx.showToast({
                        title: resData.message,
                        duration: 2000
                    })
                }
            }
        })
        /**
         * 显示个人信息
         * 1、登录成功的时候已经返回了一个student、grade、acadeny
         * 2、从全局变量获取显示即可
         */

        if (getApp().globalUserInfo == null) {
            this.setData({
                teaName: getApp().globalUserInfo_tea.teaName,
                teaPhone: getApp().globalUserInfo_tea.teaPhone,

            })
        } else {
            this.setData({
                stuId: getApp().globalUserInfo.stuId,
                stuName: getApp().globalUserInfo.stuName,
                academy: getApp().globalAcademy.acaName,
                grade: getApp().globalGrade.graName,
                stuPhone: getApp().globalUserInfo.stuPhone,
                // reserve: getApp().user_reserve,
                // honest_grade: getApp().honest_grade,
            })

            this.getReserveState();
            this.studentSelect();
            //信用检查，一次就好
            wx.request({
                url: baseURL + '/honestStudent/id',
                method: "GET",
                header: {
                    'Authorization': getApp().token
                },

                success: function (res) {
                    var resData = res.data;
                    if (resData.code != 0) {
                        wx.showModal({
                            title: "温馨提示", // 提示的标题
                            content: res.data.message, // 提示的内容
                            showCancel: false, // 是否显示取消按钮，默认true
                            // cancelText: "取消", // 取消按钮的文字，最多4个字符
                            cancelColor: "#000000", // 取消按钮的文字颜色，必须是16进制格式的颜色字符串
                            confirmText: "确定", // 确认按钮的文字，最多4个字符
                            confirmColor: "#576B95", // 确认按钮的文字颜色，必须是 16 进制格式的颜色字符串
                            success: function (res) {
                                if (res.confirm) {}
                            },

                        })
                    }
                }
            })

        }
        console.log('reserve')
        console.log(that.data.reserve)






    },
    studentSelect: function () {

        var that = this
        wx.request({
            url: baseURL + '/honestStudent/grade',
            method: "GET",
            header: {
                'Authorization': getApp().token
            },
            data: {
                "stuId": that.data.stuId
            },
            success: function (res) {
                var resData = res.data;
                if (resData.code == 0) {


                    that.setData({
                        honestGrade: resData.data.grade
                        // reserveState: resData.data.reserve.reserveState
                    })


                } else {
                    wx.showToast({
                        title: resData.message,
                        duration: 2000
                    })
                }
            }
        })

    },
    getReserveState: function () {
        // console.log("getReserveState->globalStuUseId:",getApp().globalUserInfo.globalStuUseId)
        var that = this
        //获取选座状态
        wx.request({
            url: baseURL + '/studentSeatRelation/main/reserve',
            method: "GET",
            header: {
                'Authorization': getApp().token
            },
            success: function (res) {
                console.log("reserve")
                console.log(res)
                var resData = res.data;
                if (resData.code == 0) {
                    that.setData({
                        reserve: resData.data.reserve,
                        seaNum: resData.data.seaNum,
                        floNum: resData.data.floNum
                        // reserveState: resData.data.reserve.reserveState
                    });
                    console.log("resData.data.reserve->resData.data.reserve:", resData.data.reserve)
                    getApp().user_reserve = resData.data.reserve
                    getApp().seaNum = resData.data.seaNum
                    getApp().floNum = resData.data.floNum

                }
            }
        })
    },

    dateCheck: function (hour, minute) {
        var date = new Date();
        var currentHour = date.getHours();
        var currentMinute = date.getMinutes();
        if (currentHour - hour > 0) {
            //说明时间过期
            return -1;
        }
        if (currentHour - hour == 0) {
            if (currentMinute - minute < 0) {
                return -1;
            }
        }
        return currentMinute - minute;
    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})